/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.DaftarKelasDAO;
import com.ivanbiz.dao.impl.DaftarKelasDAOImpl;
import com.ivanbiz.model.DaftarKelas;
import com.ivanbiz.model.Kelas;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author IW20149X
 */
public class JadwalKelasActionService extends SelectorComposer<Component> {

    DaftarKelasDAO daftarKelasDAO;
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
    @Wire
    Window win;
    @Wire
    Textbox transactionReference;
    @Wire
    Listbox listDaftarKelas;
    List<DaftarKelas> loadDaftarKelas;
    Kelas kelas;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        daftarKelasDAO = new DaftarKelasDAOImpl();
        kelas = (Kelas) Executions.getCurrent().getArg().get("kelas");
        loadDaftarKelas();
    }

    private void loadDaftarKelas() {
        try {
            listDaftarKelas.getItems().clear();
            int no = 0;
            int x = 0;
            loadDaftarKelas = daftarKelasDAO.getDataByEqualsOrderByBankAndNama(transactionReference.getValue());
            for (DaftarKelas daftarKelas : loadDaftarKelas) {
                no += 1;
                Listitem item = new Listitem();
                item.setValue(daftarKelas);
                item.appendChild(new Listcell(String.valueOf(no)));
                item.appendChild(new Listcell(daftarKelas.getMurid().getNama()));
                item.appendChild(new Listcell(daftarKelas.getMurid().getBank().getNama()));
                listDaftarKelas.appendChild(item);
                ++x;
            }
        } catch (Exception ex) {
            Logger.getLogger(JadwalKelasActionService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Listen("onClick=#tambah;")
    public void tambah() {
        Map map = new HashMap();
        map.put("kelas", kelas);
        map.put("loadDaftarKelas", loadDaftarKelas);
        Window wind = (Window) Executions.createComponents("/murid.zul", null, map);
        wind.setWidth("50%");
        wind.setHeight("50%");
        wind.doModal();
        loadDaftarKelas();
    }

    @Listen("onClick=#hapus;")
    public void hapus() {
        if (listDaftarKelas.getSelectedItem() == null) {
            Messagebox.show("Pilih data yang akan dihapus");
        } else {
            try {
                DaftarKelas daftarKelas = listDaftarKelas.getSelectedItem().getValue();
                daftarKelasDAO.delete(daftarKelas);
                loadDaftarKelas();
            } catch (Exception ex) {
                Logger.getLogger(JadwalKelasActionService.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
